function [ f ] = plotBandedResponsiveness( data )
%PLOTBANDED Plot the imagedata of a banded frequency analysis
%   Returns a handle to the figure
%   Input argument (data) is a structure:
%           data.bands{x}.foi
%           data.bands{x}.description
%           data.bands{x}.normalizedMeanDiff
%           data.bands{x}.stimulusVar
%           data.bands{x}.restVar
%           data.electrodeLabels
%           data.setDescription

colormap = [0 0 0.5625;0 0 0.625;0 0 0.6875;0 0 0.75;0 0 0.8125;0 0 0.875;0 0 0.9375;0 0 1; ...
    0 0.0625 1;0 0.125 1;0 0.1875 1;0 0.25 1;0 0.3125 1;0 0.375 1;0 0.4375 1;0 0.5 1;0 0.5625 1;0 0.625 1;0 0.6875 1;0 0.75 1;0 0.8125 1;0 0.875 1;0 0.9375 1;0 1 1; ...
    0.1429 1 1;0.2857 1 1;0.4286 1 1;0.5714 1 1;0.7143 1 1;0.8571 1 1;1 1 1; ...
    1 1 0.8889;1 1 0.7778;1 1 0.6667;1 1 0.5556;1 1 0.4444;1 1 0.3333;1 1 0.2222;1 1 0.1111;1 1 0; ...
    1 0.9375 0;1 0.875 0;1 0.8125 0;1 0.75 0;1 0.6875 0;1 0.625 0;1 0.5625 0;1 0.5 0;1 0.4375 0;1 0.375 0;1 0.3125 0;1 0.25 0;1 0.1875 0;1 0.125 0;1 0.0625 0;1 0 0; ...
    0.9375 0 0;0.875 0 0;0.8125 0 0;0.75 0 0;0.6875 0 0;0.625 0 0;0.5625 0 0;0.5 0 0];

numberOfChannels = length(data.electrodeLabels);

f = figure('PaperOrientation','landscape','Colormap', colormap);



plotSpacing = 0.007;
plotHeight = ((0.8 + plotSpacing) / length(data.bands)) - plotSpacing;
for b = 1:length(data.bands)
    bottom = 0.1 + (b-1)*(plotHeight+plotSpacing);
    ax = axes('Parent', f, ...
        'FontSize', 5, ...
        'Color', 'none', ...
        'CLim', [-1 1], ...
        'Box', 'on', ...
        'XTick', [], ...
        'Position', [0.03 bottom 0.9 plotHeight]);
    
    xlim(ax, [0.5 numberOfChannels+0.5]);
    step = (data.bands{b}.foi(2)-data.bands{b}.foi(1))/2;
    ylim(ax, [min(data.bands{b}.foi)-step max(data.bands{b}.foi)+step]);

    hold(ax,'all');
    image(1:numberOfChannels, data.bands{b}.foi, data.bands{b}.normalizedMeanDiff, 'Parent', ax, 'CDataMapping', 'scaled');
end



ax0 = axes('Parent', f,...
    'Position',[0.03 0.1 0.9 0.8],... % left, bottom, width, height
    'Layer', 'top', ...
    'Visible', 'on', ...
    'Color', 'none', ...
    'CLim', [-1 1], ...
    'XLim', [0.5 numberOfChannels+0.5], ...
    'XTickLabel', [], ...
    'XTick', 1.5:1:numberOfChannels-0.5, ...
    'YTick', [], ...
    'Box', 'on', ...
    'XGrid','on',...
    'FontSize', 5);
set(get(ax0,'XLabel'),'String','Electrode');
set(get(ax0,'YLabel'),'String','Frequency');
title(ax0, data.setDescription, 'FontSize',12);
colorbar('peer', ax0, ...
    'FontSize', 5, ...
    'Position', [0.95 0.1 0.015 0.8], ...
    'YAxisLocation', 'left', ...
    'YTick', -0.8:0.2:0.8, ...
    'YTickLabel', {'-80%', '-60%', '-40%', '-20%', '0%', '20%', '40%', '60%', '80%'}, ...
    'TickLength', [0 0]);

% New axes for vertical electrode labels
ax1 = axes('Parent', f,...
    'Position',[0.03 0.07 0.9 0.8],... % left, bottom, width, height
    'Layer', 'top', ...
    'Visible', 'off', ...
    'Color', 'none', ...
    'XLim', [0.5 numberOfChannels+0.5], ...
    'XTickLabel', data.electrodeLabels, ...
    'XTick', 1:length(data.electrodeLabels), ...
    'YTick', [], ...
    'Box', 'on', ...
    'XGrid','on',...
    'FontSize', 5);
set(get(ax1,'XLabel'),'String',[]);
set(get(ax1,'YLabel'),'String',[]);
xticklabel_rotate([], 90, data.electrodeLabels); % fucks up the positioning :(

end




